package com.bmps.springcloud.insuredmicroservice.config.accept;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
/**
 *  @author lyw
 *  @date 2019/07/24
 */
@Configuration
@MapperScan(basePackages = "com.bmps.springcloud.insuredmicroservice.repository.*.coresystem", sqlSessionFactoryRef = "coreSqlSessionFactory")
public class CoreDataSourceConfig {
    @Bean(name = "coreDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.core")
    public DataSource getCoreDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "coreSqlSessionFactory")
    public SqlSessionFactory coreSqlSessionFactory(@Qualifier("coreDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //xml位置
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/**/coresystem/*.xml"));
        return sqlSessionFactoryBean.getObject();

    }
    @Bean(name = "coreSqlSessionTemplate")
    public SqlSessionTemplate coreSqlSessionTemplate(@Qualifier("coreSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
